<?php

namespace Mobile\Controller;

/**
 * 我的客栈 客栈印象移动端接口
 * @modify wenhui 2016-12-06 创建
 */
class RoomSettingController extends ParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'hotel' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $hotelId = $param['hotel'];
        $userId = $param['userId'];

        //检查操作权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        $adminAuthorityLogic->check($hotelId, $userId);

        //检查客栈是否在有效期内
        $hotelLogic = A('Common/Hotel', 'Logic');
        $hotelLogic->checkHotelActive($hotelId);
        $hotelLogic->checkHotelStatus($hotelId);
    }

    /**
     * 获取我的客栈 客栈印象信息
     * @modify wenhui 2016-12-06    创建
     * @modify wenhui 2017-03-10    新增返回字段index v2.0.06
     */
    public function getImpression() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'hotel' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        $userId = $param['userId'];
        $token = $param['token'];
        $hotelId = $param['hotel'];

        $version = getVersion(array('2.0.09'));

        //检验token
        $this->checkMobileToken($userId, $token);
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        // 组装返回数据格式 获取客栈印象信息
        $data['images'] = array();
        $hotelDetail = $hotelBookLogic->getInfo($hotelId);
        if (!empty($hotelDetail['coverimageurl'])) {
                $images = array(
                'imageUrl' => MEIZHU_URL . $hotelDetail['coverimageurl'],
                'isCover' => ENABLE,
            );
            array_push($data['images'], $images);
        }
        
        // 处理客栈图片
        foreach ($hotelDetail['gallery'] as $galleryImages) {
            if (!empty($galleryImages['name'])) {
                $images = array(
                    'imageUrl' => MEIZHU_URL . $galleryImages['name'],
                    'imageId' => $galleryImages['id'],
                    'isCover' => DISABLE,
                );
                array_push($data['images'], $images);
            }
        }
        unset($galleryImages);
        // 处理客栈标签
        $data['specialTag'] = array();
        foreach ($hotelDetail['specialTag'] as $specialTags) {       
            array_push($data['specialTag'], $specialTags);
        }
        unset($specialTags);

        //  获取hotelspecial表中的数据 处理掌柜说信息
        $hotelSpecialDetail = $hotelBookLogic->getSpecialInfo($hotelId);
        $data['specialInfo'] = array();
        foreach ($hotelSpecialDetail as &$hotelSpecialItems) {
            // 处理数据
            unset($hotelSpecialItems['hotelentity_id']);
            if (!empty($hotelSpecialItems['specialimageurl'])) {
                $specialImageUrl = MEIZHU_URL . $hotelSpecialItems['specialimageurl'];
            } else {
                $specialImageUrl = '';
            }

            $hotelSpecialItem = array(
                'specialId' => $hotelSpecialItems['id'],
                'special' => $hotelSpecialItems['special'],
                'specialImageUrl' => $specialImageUrl,
                'isactive' => $hotelSpecialItems['isactive'],
            );

            if ($version >= '2.0.09') {
                $hotelSpecialItem['index'] = $hotelSpecialItems['sortindex'];
            }
            array_push($data['specialInfo'], $hotelSpecialItem);
        }
        unset($hotelSpecialItems);

        // 处理数据
        unset($hotelDetail['name']);
        unset($hotelDetail['address']);
        unset($hotelDetail['contact']);
        unset($hotelDetail['description']);
        unset($hotelDetail['longtitude']);
        unset($hotelDetail['latitude']);
        unset($hotelDetail['facility']);
        unset($hotelDetail['mobile']);
        unset($hotelDetail['telephone']);
        unset($hotelDetail['wechat']);
        unset($hotelDetail['enable']);
        unset($hotelDetail['ordertimeout']);
        unset($hotelDetail['specialimageurl']);
        unset($hotelDetail['special']);
        unset($hotelDetail['specialTag']);
        unset($hotelDetail['gallery']);
        unset($hotelDetail['coverimageurl']);

        returnJson(200, 200, $data);
    }

    /**
     * 保存个性推荐标签
     * @modify wenhui 2016-12-06    创建
     */
    public function saveSpecialTag() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'tag' => 'notnull',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }
        $tag = $param['tag'];
        if ($tag == '') {
            $tag = array();
        } else {
            $tag = explode(',', $tag);
            foreach ($tag as $tagItem) {
                $validatePass = $this->ajaxValidateReturn($this->validateUtil, $tagItem, 'nameLength15');
                if (!$validatePass) {
                    return;
                }
            }
            unset($tagItem);
        }
        // 标签最多3个
        if (count($tag) > 3) {
            returnJson(403, 202);
            return;
        }

        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经开通
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $saveResult = $hotelBookLogic->saveSpecialInfo($hotelId, null, null, $tag);
        if (empty($saveResult)) {
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 删除客栈图片
     * @modify wenhui 2016-12-06    创建
     */
    public function deleteImage() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'isCover' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        $userId = $param['userId'];
        $token = $param['token'];
        //检验token
        $this->checkMobileToken($userId, $token);

        // 校验客栈是否已经开通
        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        // 判断删除的是否是封面 1：为封面
        if ($param['isCover'] == 1) {
            $hotelDetailModel = D('HotelDetail');
            $saveResult = $hotelDetailModel->saveInfo($hotelId, null, null, null, null, '', null, null, null, null, null, null, null, null);
            if (empty($saveResult)) {
                returnJson(500, 506);
                return;
            }
        } else {
            $hotelPictureId = isset($param['imageId']) ? $param['imageId'] : null;
            $saveResult = $hotelBookLogic->deleteImage($hotelPictureId);
            if (empty($saveResult)) {
                returnJson(500, 506);
                return;
            }
        }

        returnJson(200, 200);
    }

    /**
     * 保存客栈封面图片信息
     * @modify wenhui 2016-12-06    创建
     */
    public function saveCoverImg() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'hotel' => 'notnull,notempty,id',
            'imageId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'url' => 'notnull,notempty,nameLength255',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        $userId = $param['userId'];
        $token = $param['token'];
        //检验token
        $this->checkMobileToken($userId, $token);

        // 校验客栈是否已经开通
        $hotelId = $param['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        // 先根据客栈图片的id 删除客栈图片
        $hotelPictureId = isset($param['imageId']) ? $param['imageId'] : null;
        $saveResult = $hotelBookLogic->deleteImage($hotelPictureId);
        if (empty($saveResult)) {
            return;
        }

        // 在修改封面之前 先拿出原来封面的url 保存到客栈图册中
        $hotelInfos = $hotelBookLogic->getInfo($hotelId);
        $oldCoverUrl = isset($hotelInfos['coverimageurl']) ? $hotelInfos['coverimageurl'] : '';

        if (!empty($oldCoverUrl)) {
            // 拿原来客栈图册图片 组装$url
            $urls = array();
            foreach ($hotelInfos['gallery'] as $key => $galleryItem) {
                array_push($urls, $galleryItem['name']);
            }
            unset($galleryItem);

            // 保存到房型图册中
            array_push($urls, $oldCoverUrl);
            // 描述图最多4张
            if (count($urls) > 4) {
                returnJson(403, 202);
                return;
            }

            $saveResult = $hotelBookLogic->saveGallery($hotelId, $urls);
            if (empty($saveResult)) {
                return;
            }

            // 获取新插入客栈图册的图片Id
            $imageInfo = $hotelBookLogic->getImageInfo($hotelId, $oldCoverUrl);
            if ($imageInfo === false) {
                returnJson(500, 511);
            }

            $imageId = isset($imageInfo['id']) ? $imageInfo['id'] : '';
            if (!empty($imageId)) {
                $data = array(
                    'imageId' => $imageId,
                    'imageUrl' => MEIZHU_URL . $oldCoverUrl,

                );
            }
        }

        // 修改封面图片
        $url = parse_url($param['url'])['path'];
        $hotelDetailModel = D('HotelDetail');
        $saveResult = $hotelDetailModel->saveInfo($hotelId, null, null, null, null, $url, null, null, null, null, null, null, null, null);
        if (empty($saveResult)) {
            returnJson(500, 506);
            return;
        }

        $data['coverUrl'] = MEIZHU_URL . $url;

        returnJson(200, 200, $data);
    }

    /**
     * 保存客栈图册图片
     * @modify wenhui 2016-12-06    创建
     */
    public function saveHotelImage() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (empty($validatePass)) {
            return;
        }
        $hotelId = $param['hotel'];
        $userId = $param['userId'];
        $token = $param['token'];

        //检验token
        $this->checkMobileToken($userId, $token);
        // 校验客栈是否已经开通
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
            return;
        }

        // 先拿出原来客栈图册的url 与上传的url 组装$url 一起保存到客栈图册中
        $hotelInfos = $hotelBookLogic->getInfo($hotelId);
        $urls = array();
        foreach ($hotelInfos['gallery'] as $key => $galleryItem) {
            array_push($urls, $galleryItem['name']);
        }
        unset($galleryItem);

        if (!isset($_FILES['file'])) {
            returnJson(500, 511);
        }

        // 调用上传图片函数 上传成功返回url 失败返回null
        $newUrl = mobileUpload($_FILES['file'], 'bookGallery');
        array_push($urls, $newUrl);

        // 描述图最多4张
        if (count($urls) > 4) {
            returnJson(403, 415);
            return;
        }
        // 保存客栈图册
        $saveResult = $hotelBookLogic->saveGallery($hotelId, $urls);
        if (empty($saveResult)) {
            return;
        }

        if (!isset($newUrl)) {
            returnJson(500, 511);
        }

        // 获取新插入客栈图册的图片Id
        $imageInfo = $hotelBookLogic->getImageInfo($hotelId, $newUrl);
        if ($imageInfo === false) {
            returnJson(500, 511);
        }
        $imageId = isset($imageInfo['id']) ? $imageInfo['id'] : '';

        $data = array();
        if (!empty($imageId)) {
            $data = array(
                'imageId' => $imageId,
                'imageUrl' => MEIZHU_URL . $newUrl,
            );
        }

        returnJson(200, 200, $data);
    }

    /**
     * 添加掌柜说信息
     * @modify wenhui 2016-12-06 创建
     * @modify wenhui 2017-03-10 v2.0.09 新增时 没设置掌柜说 则设置掌柜说
     */
    public function addSpecialInfo() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $userId = $param['userId'];
        $token = $param['token'];
        $hotelId = $param['hotel'];

        $version = getVersion(array('2.0.09'));

        //检验token
        $this->checkMobileToken($userId, $token);
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }

        if (!isset($_FILES['file'])) {
            returnJson(500, 511);
        }
        // 调用上传图片函数 上传成功返回url 失败返回null
        $newUrl = mobileUpload($_FILES['file'], 'bookSpecial');

        $url = isset($newUrl) ? $newUrl : '';
        // 添加个性推荐信息 返回插入数据的ID
        if ($version >= '2.0.09') {
            $specialData = $hotelBookLogic->addSpecialInfo($hotelId, $url, true);
        } else {
            $specialData = $hotelBookLogic->addSpecialInfo($hotelId, $url, false);
        }
   
        if (empty($specialData)) {
            returnJson(500, 508);
        }

        $specialData['specialImageUrl'] = isset($specialData['specialImageUrl']) && !empty($specialData['specialImageUrl']) ? MEIZHU_URL . $specialData['specialImageUrl'] :'';

        returnJson(200,200,$specialData);
    }

    /**
     * 修改掌柜说图片
     * @modify wenhui 2016-12-06 创建
     */
    public function saveSpecialImage() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $userId = $param['userId'];
        $token = $param['token'];
        $hotelId = $param['hotel'];
        //检验token
        $this->checkMobileToken($userId, $token);
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403, 409);
        }

        if (!isset($_FILES['file'])) {
            returnJson(500, 511);
        }
        // 调用上传图片函数 上传成功返回url 失败返回null
        $newUrl = mobileUpload($_FILES['file'], 'bookSpecial');

        $url = isset($newUrl) ? $newUrl : '';
        $specialId = $param['specialId'];
        $returnResult = $hotelBookLogic->saveSpecial($hotelId, $specialId, $url, null);
        if (empty($returnResult)) {
            return;
        }

        $data = array();
        if (!empty($url)) {
            $data['imageId'] = $specialId;
            $data['imageUrl'] = MEIZHU_URL . $url;
        }

        returnJson(200, 200, $data);
    }

    /**
     * 修改掌柜说描述
     * @modify wenhui 2016-11-15  创建
     *
     */
    public function saveSpecialInfo() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',
            'special' => 'notnull,notempty,nameLength100',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $userId = $param['userId'];
        $token = $param['token'];
        $hotelId = $param['hotel'];
        //检验token
        $this->checkMobileToken($userId, $token);
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }

        $special = $param['special'];
        $specialId = $param['specialId'];
        // 修改掌柜说描述
        $specialId = $hotelBookLogic->saveSpecial($hotelId, $specialId, null, $special);
        if (empty($specialId)) {
            return;
        }

        returnJson(200,200);
    }

    /**
     * 我的客栈 设置掌柜说
     * @modify wenhui 2016-12-06 创建
     */
    public function saveManage() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $version = getVersion(array('2.0.09'));
        if ($version >= '2.0.09') {
            $validateRule = array(
                'index' => 'notnull,notempty',
            );
            $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
            if (!$validatePass) {
                return;
            }
        }

        $userId = $param['userId'];
        $token = $param['token'];
        $hotelId = $param['hotel'];
        $index = isset($param['index']) ? $param['index'] : null;
        //检验token
        $this->checkMobileToken($userId, $token);
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }

        $specialId = $param['specialId'];
        // 设置掌柜说
        $saveResult = $hotelBookLogic->saveManageInfo($hotelId, $specialId, $index);
        if (!$saveResult) {
            return;
        }

        returnJson(200,200);
    }

    /**
     * 删除掌柜说信息
     * @modify wenhui 2016-12-06 创建
     */
    public function deleteSpecialInfo() {
        $param = I('post.');
        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'specialId' => 'notnull,notempty,id',
        );
        $validatePass = $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        if (!$validatePass) {
            return;
        }

        $userId = $param['userId'];
        $token = $param['token'];
        $hotelId = $param['hotel'];
        //检验token
        $this->checkMobileToken($userId, $token);
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        // 校验是否已经通过
        $isEnable = $hotelBookLogic->checkEnable($hotelId);
        if (!$isEnable) {
            returnJson(403,409);
        }

        $specialId = $param['specialId'];
        // 如果此推荐信息已经设置为掌柜说  则不能删除
        $hotelSpecialDetail = $hotelBookLogic->getOneSpecialInfo($specialId);
        if ((int)$hotelSpecialDetail['isactive'] === ENABLE) {
            returnJson(500,510);
            return false;
        }

        // 删除个性推荐信息
        $deleteResult = $hotelBookLogic->deleteSpecialInfos($specialId);
        if (!$deleteResult) {
            return false;
        }

        returnJson(200, 200);
    }

    /**
     * 掌柜说排序
     * @modify wenhui 2017-03-09    创建
     */
    public function manageSortIndex() {
        $param = I('post.');

        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'token' => 'notnull,notempty',
            'hotel' => 'notnull,notempty,id',
            'indexs' => 'notnull,notempty',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        $validateRule = array(
            'specialId' => 'notnull,notempty,id',
            'index' => 'notnull,notempty',
        );
        $indexs = json_decode(stripslashes(htmlspecialchars_decode(html_entity_decode($param['indexs']))), true);
        foreach ($indexs as $key => $sortIndexs) {
            $this->ajaxValidateReturn($this->validateUtil, $sortIndexs, $validateRule);
        }
        unset($sortIndexs);
 
        $hotel = $param['hotel'];
        $indexValues = array_column($indexs,'index');
        // 校验索引值是否重复
        $indexValues = array_column($indexs, 'index');
        if (count($indexValues) != count(array_unique($indexValues))) {
            returnJson(403, 212);
        }

        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        try {
            $saveResult = $hotelBookLogic->manageSortIndex($hotel, $indexs);
        } catch (\Exception $e) {
            $error = explode(',', $e->getMessage());
            returnJson($error[0], $error[1]);
        }

        returnJson(200, 200);
    }
}